package com.atguigu.gulimall.product.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.atguigu.gulimall.common.utils.PageUtils;
import com.atguigu.gulimall.common.utils.Query;
import com.atguigu.gulimall.product.service.CategoryBrandRelationService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gulimall.product.dao.BrandDao;
import com.atguigu.gulimall.product.entity.BrandEntity;
import com.atguigu.gulimall.product.service.BrandService;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {
    @Autowired
    private BrandDao brandDao;
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;


        @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String name =(String) params.get("name");
        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),
                new QueryWrapper<BrandEntity>()
                        .like(StringUtils.isNotEmpty(name),"name",name)
        );

        return new PageUtils(page);
    }

    /**
     *我们不止更新品牌表，还需要保障冗余表字段一致
     * @param brand
     */
    @Override
    public void updateDetail(BrandEntity brand) {
            this.updateById(brand);
        if (ObjectUtil.isNotEmpty(brand.getName())) {
            //同步更新关联表数据
            categoryBrandRelationService.updateBrand(brand.getBrandId(),brand.getName());
            //TODO 更新其他关联

        }
    }

}